import pygame

from pgu import gui

screen = pygame.display.set_mode((960,640))

app = gui.App()

class Editor(gui.Container):

    def __init__(self, **params):
        gui.Container.__init__(self, **params)
        
        self.side_bar = SideBar(self,width=320,height=640)
        self.viewer = ShipViewer(self,width=640,height=640)
        
        
        self.od = gui.FileDialog("Load image","Load")
        self.od.connect(gui.CLOSE, self.load_ship, self.od)
        
        self.add(self.side_bar,0,0)
        self.add(self.viewer,320,0)
        
    def show_load_dialog(self, e, **params):
        self.od.open()
        
    def load_ship(self, e, **params):
        print "Loading ship: ",self.od.value
        self.viewer.image = pygame.image.load(self.od.value)
        
class SideBar(gui.Table):
    
    def __init__(self, editor, **params):
        gui.Table.__init__(self, **params)

        self.editor = editor
        
        # Foreground color
        fg = (255,255,255)
        
        # Add a row
        self.tr()
        # And a title label
        self.td(gui.Label("Spanky - The Ship Editor!", color = fg), colspan = 2)
        
        # Another row
        self.tr()
        # A lable for our name field
        self.td(gui.Label("Name:", color = fg), align=1)
        # Our name field
        self.td(gui.Input(width=120), disabled=True)
        
        # Another row
        self.tr()
        # A load button. Very useful button, that.
        l = gui.Button("Load",width=48,height=24)
        self.td(l)
        
        l.connect(gui.CLICK,editor.show_load_dialog,l)
        
        
class ShipViewer(gui.Widget):
    
    def __init__(self, editor, width, height, **params):
        gui.Widget.__init__(self,width=width, height=height, **params)
        self.editor = editor
        self.image = pygame.Surface((32,32))
        self.draw_rect = pygame.Rect(0,0,32,32)
        
    def paint(self, surf):
        surf.fill((0,0,0))
        surf.blit(self.image, self.draw_rect)
        
    @property
    def image(self):
        return self.image    
    
    @image.setter
    def set_image(self, image):
        self.image = image
        self.draw_rect.size = self.image.get_rect().size
        self.draw_rect.center = (320,320)

        
    def event(self,e):
        if not self.image: return

        if e.type == gui.MOUSEBUTTONDOWN:
            print "Mouse button '", e.button, "' pressed at: ", e.pos
        if e.type == gui.MOUSEMOTION:
            pass
            #print e.pos
        if e.type is gui.MOUSEBUTTONUP:
            print "Mouse button '", e.button, "' released at: ", e.pos
        
e = Editor()

app.connect(gui.QUIT, app.quit)

app.run(e)
